package com.mark.springboot.util;

import com.lowagie.text.Font;
import com.lowagie.text.*;
import com.lowagie.text.rtf.RtfWriter2;
import com.mark.springboot.dao.QueryDao;
import com.mark.springboot.domain.TableFileds;
import com.mark.springboot.domain.Tables;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.FileOutputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 创建word文档 步骤:
 * 1,建立文档
 * 2,创建一个书写器
 * 3,打开文档
 * 4,向文档中写入数据
 * 5,关闭文档
 *
 **/
@Service
public class DateToWordUtil {

    @Autowired
    QueryDao queryDao;


    public void toWord(List<Tables> tables, String fileName, String dbName) {// 创建word文档,并设置纸张的大小
        Document document = new Document(PageSize.A4);
        try {
            // 创建word文档
            RtfWriter2.getInstance(document, new FileOutputStream(fileName));
            document.open();
            LinkedHashMap<String, List<Tables>> collect = tables.stream().collect(Collectors.groupingBy(Tables::getTableSchema, LinkedHashMap::new, Collectors.toList()));
            for (Map.Entry<String, List<Tables>> entry : collect.entrySet()) {
                String schema = entry.getKey();
                Paragraph ph = new Paragraph();
                Font f = new Font();
                Paragraph p = new Paragraph(schema + "库表字段文档", new Font(Font.NORMAL, 24, Font.BOLDITALIC, new Color(0, 0, 0)));
                p.setAlignment(1);
                document.add(p);
                ph.setFont(f);
                tables = entry.getValue();
                for (int i = 0; i < tables.size(); i++) {
                    String table_name = tables.get(i).getName();
                    String table_comment = tables.get(i).getComment();
                    String table_schema = tables.get(i).getTableSchema();

                    List<TableFileds> fileds = queryDao.getTable(tables.get(i).getTableSchema(),tables.get(i).getName());

                    String all = (i + 1) + " 表名:" + table_schema + "." + table_name + ":" + table_comment;

                    Table table = new Table(6);

                    document.add(new Paragraph(""));

                    table.setBorderWidth(1);

                    // table.setBorderColor(Color.BLACK);

                    table.setPadding(0);

                    table.setSpacing(0);


                    // 单元格
                    Cell cell = new Cell("序号");
                    setCell(table, cell);


                    cell = new Cell("字段名");
                    setCell(table, cell);


                    cell = new Cell("数据类型");
                    setCell(table, cell);


                    cell = new Cell("isNull");
                    setCell(table, cell);


                    cell = new Cell("是否为主键");
                    setCell(table, cell);


                    cell = new Cell("备注");
                    setCell(table, cell);

                    // 表头结束
                    table.endHeaders();
                    // 表格的主体
                    for (int k = 0; k < fileds.size(); k++) {

                        setCell2(table, (k + 1) + "");
                        setCell2(table, fileds.get(k).getField());
                        setCell2(table, fileds.get(k).getType());
                        setCell2(table, fileds.get(k).getNull());
                        setCell2(table, fileds.get(k).getKey());
                        setCell2(table, fileds.get(k).getComment());

                    }
                    Paragraph pheae = new Paragraph(all);
                    pheae.setAlignment(1);
                    //写入表说明
                    document.add(pheae);
                    //生成表格
                    document.add(table);
                }
            }

            document.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 表头居中
     * @param table
     * @param cell
     */
    private void setCell(Table table, Cell cell) {
        /*
         * 添加表头的元素，并设置表头背景的颜色
         */
        Color chade = new Color(176, 196, 222);
        cell.setBackgroundColor(chade);
        cell.setHeader(true);
        cell.setUseAscender(true);
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
        //水平居中
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        //垂直居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);
    }

    /**
     * 表格内容居中
     * @param table
     * @param str
     */
    private void setCell2(Table table, String str) {
        Cell cell = new Cell(str);
        cell.setUseAscender(true);
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
        //水平居中
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        //垂直居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);
    }
}
